﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECOSYSTEM_BL;
using ECOSYSTEM_Dominio;

namespace ECOSYSTEMPRESENTACION
{
    public partial class frmTransportista : Form
    {
        public frmTransportista()
        {
            InitializeComponent();
        }
        Transportista elTransportista = new Transportista();
        TransportistaBL elTransportistaBL = new TransportistaBL();
        private Transportista ObtenerObjetoGuardar() {
            elTransportista.RUCTransp = txtRUCTransp.Text.Trim();
            elTransportista.RazonSoacialTransp = txtNameTrans.Text.Trim();
            elTransportista.DirecTransp = txtAdressTransp.Text.Trim();
            elTransportista.TelefTransp = txtPhoneTransp.Text.Trim();
            elTransportista.EmailTransp = txtMailTransp.Text.Trim();
            elTransportista.FecRegTransp = dtpFecReg.Text.ToString();
            return elTransportista;
        }
        private Boolean guardarTransportista() {
            Transportista elTransportista = ObtenerObjetoGuardar();
            elTransportistaBL.guardarTransportista(elTransportista);
            return true;
        }
        private Transportista ObtenerObjetoActualizar()
        {
            elTransportista.TransportistaID = Convert.ToInt32(lblCodigoID.Text);
            elTransportista.RUCTransp = txtRUCTransp.Text.Trim();
            elTransportista.RazonSoacialTransp = txtNameTrans.Text.Trim();
            elTransportista.DirecTransp = txtAdressTransp.Text.Trim();
            elTransportista.TelefTransp = txtPhoneTransp.Text.Trim();
            elTransportista.EmailTransp = txtMailTransp.Text.Trim();
            return elTransportista;
        }
        private Boolean ActualizarTransportista() {
            Transportista elTransportista = ObtenerObjetoActualizar();
            if (txtRUCTransp.Text == lblRucAnterior.Text) {
                elTransportistaBL.ActualizarTransportista(elTransportista, "");
            }
            if (txtRUCTransp.Text != lblRucAnterior.Text) {
                elTransportistaBL.ActualizarTransportista(elTransportista, txtRUCTransp.Text);
            }
            return true;
        }
        private void llenarDGVListaTransportista() {
            if (dgvListaTrans.DataSource == null) {
                dgvListaTrans.DataSource = elTransportistaBL.ObtenerListaTransportista();
            }
        }
        private void LimpiarCampos() {
            txtRazSocTransFiltrar.Text = "";
            txtNameTrans.Text = "";
            txtRUCTransp.Text = "";
            txtPhoneTransp.Text = "";
            txtAdressTransp.Text = "";
            txtMailTransp.Text = "";
            txtRazSocTransFiltrar.BackColor = Color.White;
        }
        private void AplicarFiltro()
        {
            int ret = filtrar_dgvListaProducto("RazonSocial", txtRazSocTransFiltrar.Text.Trim(), bsFiltroTrans, dgvListaTrans);
            if (ret == 0)
            {
                txtRazSocTransFiltrar.BackColor = Color.Red;
            }
            else
            {
                txtRazSocTransFiltrar.BackColor = Color.White;
            }
        }
        private int filtrar_dgvListaProducto(String Columna, String texto, BindingSource Binding, DataGridView data)
        {
            if (bsFiltroTrans.DataSource == null) { return 0; }
            try
            {
                String filtro = String.Empty;
                filtro = "LIKE '" + texto.Trim() + "%'";
                if (filtro != String.Empty)
                {
                    filtro = "[" + Columna + "]" + filtro;
                }
                Binding.Filter = filtro;
                data.DataSource = Binding.DataSource;
                return Binding.Count;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            return 0;
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNewTransp_Click(object sender, EventArgs e)
        {
            try
            {
                guardarTransportista();
                MessageBox.Show("Transporte : "+txtNameTrans.Text+" REGISTRADO CORRECTAMENTE");
                LimpiarCampos();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnUpdateTransp_Click(object sender, EventArgs e)
        {
            try
            {
                ActualizarTransportista();
                MessageBox.Show("Transporte : " + txtNameTrans.Text + " ACTUALIZADO CORRECTAMENTE");
                LimpiarCampos();
                rbNewTransp.Checked = true;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void rbNewTransp_CheckedChanged(object sender, EventArgs e)
        {
            if (rbNewTransp.Checked == true)
            {
                dgvListaTrans.DataSource = null;
                LimpiarCampos();
                txtNameTrans.Select();
                btnNewTransp.Enabled = true;
            }
            else { btnNewTransp.Enabled = false; }
        }

        private void rbUpdateTransp_CheckedChanged(object sender, EventArgs e)
        {
            if (rbUpdateTransp.Checked == true)
            {
                LimpiarCampos();
                bsFiltroTrans.DataSource = elTransportistaBL.ObtenerListaTransportista();
                llenarDGVListaTransportista();
                txtRazSocTransFiltrar.Select();
                btnUpdateTransp.Enabled = true;
            }
            else { btnUpdateTransp.Enabled = false; }
        }

        private void frmTransportista_Load(object sender, EventArgs e)
        {
            rbNewTransp.Checked = true;
        }

        private void txtRazSocTransFiltrar_TextChanged(object sender, EventArgs e)
        {
            AplicarFiltro();
        }

        private void dgvListaTransportista_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int nroFila = Int32.Parse(e.RowIndex.ToString());
                txtNameTrans.Text = Convert.ToString(dgvListaTrans["RazonSocial", nroFila].Value);
                txtRUCTransp.Text = Convert.ToString(dgvListaTrans["RUCTrans", nroFila].Value);
                lblRucAnterior.Text = Convert.ToString(dgvListaTrans["RUCTrans", nroFila].Value);
                lblCodigoID.Text = Convert.ToString(dgvListaTrans["TransportistaID", nroFila].Value);
                txtPhoneTransp.Text = Convert.ToString(dgvListaTrans["Telefono", nroFila].Value);
                txtAdressTransp.Text = Convert.ToString(dgvListaTrans["Direccion", nroFila].Value);
                txtMailTransp.Text = Convert.ToString(dgvListaTrans["Email", nroFila].Value);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
    }
}
